home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / Random.st < prev    next >
Text File  |  1991-09-12  |  3KB  |  117 lines

  1. "======================================================================
  2. |
  3. |   Random number Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     19 Sep 89      Converted to use real method categories.
  34. |
  35. | sbyrne      3 Jul 89      created.
  36. |
  37. "
  38.  
  39. Stream subclass: #Random
  40.        instanceVariableNames: 'seed'
  41.        classVariableNames: ''
  42.        poolDictionaries: ''
  43.        category: nil.
  44.  
  45. Random comment: "Here's a random comment :-)"
  46. 'My instances are generator streams that produce random numbers, which are 
  47. floating point values between 0 and 1.'!
  48.  
  49. !Random class methodsFor: 'instance creation'!
  50.  
  51. new
  52.     ^self basicNew setSeed
  53. !!
  54.  
  55.  
  56.  
  57. !Random methodsFor: 'testing'!
  58.  
  59. chiSquare
  60.     "returns on Sun3 93.40000000000009"
  61.     ^self chiSquare: 1000 range: 100
  62. !
  63.  
  64. chiSquare: n range: r
  65.     | f t seed |
  66.     seed _ 1234567.
  67.     f _ Array new: r + 1.
  68.     1 to: r + 1 do: [ :i | f at: i put: 0 ].
  69.     n timesRepeat:
  70.     [ seed _ (seed * 31415821) + 1 bitAnd: 16r3FFFFFFF.
  71.           t _ seed \\ r.
  72.       f at: t + 1 put: (f at: t + 1) + 1 ].
  73.     t _ 0.
  74.     1 to: r do: [ :i | t _ t + (f at: i) squared ].
  75.     ^r asFloat * t / n - n
  76.  
  77. !!
  78.  
  79.  
  80. !Random methodsFor: 'basic'!
  81.  
  82. atEnd
  83.     ^false
  84. !
  85.  
  86. next
  87.     | value |
  88.     "From Sedgewick's 'Algorithms', based on Lehmer's method"
  89.     seed _ (seed * 31415821) + 1 bitAnd: 16r3FFFFFFF.
  90.     ^seed / 16r3FFFFFFF.0
  91. !
  92.  
  93. nextPut: value
  94.     self shouldNotImplement
  95. !
  96.  
  97. next: anInteger
  98.     | collection |
  99.     collection _ OrderedCollection new.
  100.     anInteger timesRepeat: [ collection add: self next ]. 
  101.     ^collection
  102. !
  103.  
  104. nextMatchFor: aNumber
  105.     ^self next = aNumber
  106. !!
  107.  
  108.  
  109.  
  110. !Random methodsFor: 'private'!
  111.  
  112. setSeed
  113.     seed _ Time secondClock
  114. !!
  115.